<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\payment\api;


    use app\common\controller\API;
    use app\payment\logic\OmniPayAliPay;
    use app\payment\logic\OmniPayWechatPay;
    use app\payment\logic\PayBase;
    use app\payment\logic\PayWechatOriginal;
    use app\payment\model\PaymentBaseModel;
    use app\payment\model\PaymentOrderModel;

    /**
     * 充值支付类（支付接入）
     * Class Pay
     *
     * @package app\payment\api
     */
    class Pay extends API
    {

        /**
         * 支付
         * api POST /payment/pay/start
         *
         * @param int $pay_type 支付类型（1支付宝2微信）
         * @param int $money    支付金额
         *
         * @return array|int|mixed
         */
        public function start()
        {

            $data = $this->request->param();
            $this->check_validate($data, 'Pay.start');

            $pro_price = module_config('payment.pro_price');

            $data['pro_price'] = $data['money'] * $pro_price;
            $data['true_money'] = $data['money'] - $data['pro_price'];
            $data['member_id'] = env('member_id');;
            $data = PaymentOrderModel::CreatePayOrder($data);
            if ($data['is_ok']) {
                unset($data['is_ok']);
                switch ($data['pay_type']) {
                    case PayBase::PAY_TYPE_ALIPAY://支付宝支付
                        return OmniPayAliPay::send($data['pay_sn'], $data['money']);
                        break;
                    case PayBase::PAY_TYPE_WXPAY;//微信支付
                        return (new PayWechatOriginal)->send($data['pay_sn'], $data['money']);
                        return OmniPayWechatPay::send($data['pay_sn'], $data['money']);
                        break;
                    default:
                }
            }
        }

        /**
         * 充值提示
         * api POST /payment/pay/pay_config
         */
        public function pay_config()
        {
            //            $base_config               = module_config('base');
            $tips['title'] = '充值规则';
            $tips['tips'] = ['1:充值金额最小100。', '2:充值手续费0%'];
            $tips['tips'] = ['1:充值金额最小100。', '2:充值手续费0%'];
            $base_config['tips'] = $tips;
            $module_config = module_config('payment');
            $module_config['pay_type'] = isset($module_config['pay_type']) && $module_config['pay_type'] != "" ? $module_config['pay_type'] : [];
            $base_config['pay_type'] = ['alipay' => ['name' => '支付宝', 'is_pay' => in_array(PaymentBaseModel::PAY_VIEW_ALIPAY, $module_config['pay_type'])], 'wxpay' => ['name' => '微信', 'is_pay' => in_array(PaymentBaseModel::PAY_VIEW_WECHATPAY, $module_config['pay_type'])],];
            $this->result($base_config);
        }


    }